import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import FormModal from '#cpt/temp/FormModal'
import Confirm from '#cpt/temp/Confirm'
import { MultipleType,CheckType,BoolStatus } from '@views/novel-coro-system/test-info/common/defaultData'
// ===================================================================== global declare
const { $fn, $http, $Lazy, globalConfig } = window
const { COVIDUrl } = globalConfig
// ===================================================================== Lazy
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const $time 				= $Lazy.file(()=>import('@com/utils/time'))
const ModifyTimeModal 		= $Lazy.state(()=>import('./modify-time-modal'))
const PushReportModal 	= $Lazy.state(()=>import('./push-report-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '报告时间',			names: ['report_at_start','report_at_end'], type:'date-range', name:'jz-time' },
			{ label: '报告单编号',		name: 'report_num' },
			{ label: '标本条码',			name: 'spec_code' },
			{ label: '医院',				name: 'hosp_id',	type: 'select', loading:true,	idStr:'uuid',	nameStr:'hospName' },
			{ label: '姓名',				name: 'patient_name' },
			{ label: '证件号',			name: 'card_no' },
			{ label: '采集类型',			name: 'isMultiple',			type:'select',		data:MultipleType },
			{ label: '受检类型',			name: 'checkType',			type:'select',		data:CheckType },
			{ label: '接收时间',			names: ['check_time_start','check_time_end'], type:'date-range', name:'jz-time1' },
		],
		formData:[
			{ label:'是否延用上次报告时间',	name: 'LastReportTime', 	type: 'radio', 	full:true, 	value:0, data:BoolStatus },
		]
	}
	
	model = {}
	permissions = {push:1,reportstatecb:1,updatereportat:1,download:1,infoPush:1 }
	
	componentDidMount(){
		const { ban_btn } = $fn.local('permissions')
		if($fn.hasArray(ban_btn)){
			if(ban_btn.includes('reports.push')){		// 补推
				this.permissions.push = 0
			}
			if(ban_btn.includes('caseinfo.reportstatecb')){		// 批量召回
				this.permissions.reportstatecb = 0
			}
			if(ban_btn.includes('caseinfo.updatereportat')){		// 修改报告时间
				this.permissions.updatereportat = 0
			}
			if(ban_btn.includes('codeinfo.download')){		// 导出
				this.permissions.download = 0
			}
			if(ban_btn.includes('caseinfo.push')){		// 推送到健康码
				this.permissions.infoPush = 0
			}
		}
		
		this.getSelect()
	}
	onTime = reset => {
		 $time.then(f=>{
			const arr = f.duration['2-9-now']()
			this.resetModel = { report_at_start: arr[0], report_at_end: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			this.fetch()
		})
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 医院
			this.hostData = await f.getCache({ api:COVIDUrl+'Hospital/query',param:{page:1,page_num:10000,status:1} } )
			$fn.setRow(forms, 'hosp_id', this.hostData )
			this.setState({ forms })
			this.onTime()
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,COVIDUrl+'caseinfo/query',{param:{report_state:2,status:1},COVID:true})
	
	// 表单
	cols = [
		{ title: '报告单编号',		dataIndex: 'reportNum', 		...$fn.table.spec_num },
		{ title: '标本条码', 		dataIndex: 'specCode', 			...$fn.table.spec_code },
		{ title: '姓名', 			dataIndex: 'patientName', 		...$fn.table.name  },
		{ title: '所属医院', 		dataIndex: 'hospId', 			width:200,				render: text => $fn.getState(this.hostData,text,'hospName','uuid') },
		{ title: '证件号', 			dataIndex: 'cardNo', 			...$fn.table.idcard },
		{ title: '送检科室', 		dataIndex: 'departmentName', 	width:180 },
		{ title: '受检类型', 		dataIndex: 'checkType', 		width:100,	render: text => $fn.getState(CheckType,text) },
		{ title: '采集类型', 		dataIndex: 'isMultiple', 		width:100,	render: text => $fn.getState(MultipleType,text) },
		{ title: '报告时间', 		dataIndex: 'reportAt', 		...$fn.table.time },
		{ title: '接收时间', 		dataIndex: 'checkTime', 		...$fn.table.time },
		TableControls.controls.call(this, { width: 100})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, reportNum }) => {
		const {reportstatecb } = this.permissions
		return [
			TableControls.pushTo.call(this,'预览', `preview-report?report_num=${reportNum}`),
			reportstatecb && Controls.click.call(this,'召回', ()=>{
				this.submit = { uuids:uuid }
				this.formRef.setValue({LastReportTime:0})
				this.modal.open({formRef: this.formRef})
			})
		]
	}
	// 操作按钮
	controls = () => {
		const { selectedKeys, selectedRows } = this.state
		const uuids = selectedKeys.join(',')
		const reportNum = selectedRows.map(v=>v.reportNum).join(',')
		const {push,reportstatecb,updatereportat,download,infoPush } = this.permissions
		return [
			Controls.pushTo.call(this,'预览', `preview-report?report_num=${reportNum}`, {isKeys:true}),
			reportstatecb && Controls.click.call(this,'批量召回', ()=>{
				this.submit = { uuids }
				this.formRef.setValue({LastReportTime:0})
				this.modal.open({formRef: this.formRef})
			},{isKeys:1}),
			push && Confirm.call(this,'报告单补推',COVIDUrl+'reports/push',{query:{uuids},isKeys:1}),
			updatereportat && Controls.click.call(this,'修改报告时间',()=>this.modifyTimeRef.open(uuids),{isKeys:1}),
			infoPush && Controls.click.call(this,'推送到天府健康码',()=>this.pushReportRef.open(uuids),{isKeys:1}),
			download && Controls.click.call(this,'报告导出', ()=>{
				const param = {...this.model,report_state:2}
				$http.submit(null, COVIDUrl+'caseinfo/download', { param }).then((data)=>{
					if(data){
						window.open(`${COVIDUrl}download?rand=${data}`)
					}
				})
			},{ghost:true,code:'F1'})
		]
	}
	render(){
		return (
			<>
				{/* 搜索 */}
				{ SearchForm.call(this,{ timeStr:'2-9-now' }) }
				{/* 表格 */}
				{ Table.call(this) }
				{/* 召回 */}
				{FormModal.call(this,{ title:'召回', labelWidth:'s12', width:500, api:COVIDUrl+'caseinfo/reportStateCb',enter:false,
					onRefresh: ()=>this.setState({selectedKeys:[],selectedRows:[]})
				})}
				{/* 修改报告时间 */}
				<ModifyTimeModal onRef={ref=>this.modifyTimeRef=ref} onRefresh = {this.fetch} />
				{/* 推送到天府健康码 */}
				<PushReportModal onRef={ref=>this.pushReportRef=ref} onRefresh = {()=>{
					this.setState({selectedKeys:[],selectedRows:[]})
				}} />
			</>
		)
	}
}

export default Index